package fr.inserm.umr915.vcfserver.bio;

import java.util.Comparator;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.DatabaseEntry;


public class ChromPosition
	implements Comparable<ChromPosition>
	{
	private String chromosome;
	private int position;
	
	public ChromPosition(String chromosome, int position)
		{
		this.chromosome = chromosome;
		this.position = position;
		}
	
	public String getChromosome() {
		return chromosome;
		}
	public int getPosition()
		{
		return position;
		}
	
	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result + chromosome.hashCode();
		result = prime * result + position;
		return result;
		}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ChromPosition other = (ChromPosition) obj;
		return position==other.position && chromosome.equals(other.chromosome);
		}
	
	@Override
	public int compareTo(ChromPosition o)
		{
		int i=getChromosome().compareTo(o.getChromosome());
		if(i!=0) return i;
		return getPosition()-o.getPosition();
		}
	
	@Override
	public String toString()
		{
		return getChromosome()+":"+getPosition();
		}

	
	public static class Binding extends TupleBinding<ChromPosition>
		{
		public Binding()
			{	
			}
		
		@Override
		public ChromPosition entryToObject(TupleInput in)
			{
			String c=in.readString();
			int n=in.readInt();
			return new ChromPosition(c, n);
			}
		
		@Override
		public void objectToEntry(ChromPosition o, TupleOutput out)
			{
			out.writeString(o.getChromosome());
			out.writeInt(o.getPosition());
			}
		}
	public static class Sorter implements Comparator<byte[]>
		{
		public Sorter()
			{
			}
		@Override
		public int compare(byte[] o1, byte[] o2)
			{
			Binding b=new Binding();
			ChromPosition p1= b.entryToObject(new DatabaseEntry(o1));
			ChromPosition p2= b.entryToObject(new DatabaseEntry(o2));
			return p1.compareTo(p2);
			}
		}
	}
